//添加表格数据
export const addGroup = async(req,res)=>{
    const { name  } = req.body
    if(!name) return res.fail('用户名不能为空')
    try{
        const result = {
            name,
            roles: JSON.stringify([])
        }
        await req.db.group.create({
            data: result
        })
        res.send({success: true, message: '添加成功'})
    }catch(e){
        console.log(e)
        res.fail({success: false, message: '添加失败'})
    }
}

//获取用户组数据(分页)
export const  getAllGroup = async(req,res)=>{
    let { limit=7, page=1 } = req.body
    limit = Number(limit)
    page = Number(page)
    try{
        const num = await req.db.group.findMany({
            take: limit,
            skip: (page-1)*limit
        })
        const total = await req.db.group.count()
        res.send({success:true, data: num ,info: total})
    }catch(e){
        console.log(e)
        res.fail({success:false, message: '查询失败'})
    }
}
// 获取用户组数据
export const  getAllGroupTwo = async(req,res)=>{
    try{
        const num = await req.db.group.findFirst()
        res.send({success:true, data: num })
    }catch(e){
        console.log(e)
        res.fail({success:false, message: '查询失败'})
    }
}

//删除用户组
export const delGroup = async(req,res)=>{
    const { id  } = req.body
    if(!id) return res.fail('id不能为空')
    try{
        await req.db.group.delete({
            where:{
                id:Number(id)
            }
        })
        res.send({success: true, message: '删除成功'})
    }catch(e){
        console.log(e)
        res.fail({success: false, message: '删除失败'})
    }
}
//修改用户组
export const updateGroup = async(req,res)=>{
    const { name, id } = req.body
    if(!id) return res.fail('id不能为空')
    try{
        const result = {}
        if(name) result.name = name
        await req.db.group.update({
            data:result,
            where:{
                id:Number(id)
            }
        })
        res.send({success: true, message: '更新成功'})
    }catch(e){
        console.log(e)
        res.fail({success: false, message: '更新失败'})
    }
}

//批量删除
export const delManyGroup = async(req,res)=>{
    let { id  } = req.body
    if(!id) return res.fail('id不能为空')
    try{
        await req.db.group.deleteMany({
            where:{
                id: {
                    in:id
                },
            }
        })
        res.send({success: true, message: '删除成功'})
    }catch(e){
        console.log(e)
        res.fail({success: false, message: '删除失败'})
    }
}

//用户组的权限
export const bindRoles = async(req,res)=>{
    const { groupid, ids } = req.body
    if(!groupid) return res.fail('用户id不能为空')
    if(!ids) return res.fail('权限id不能为空')
    try{
        const arrStr = JSON.stringify(ids.split(','))
        await req.db.group.update({
            where:{
                id:Number(groupid)
            },
            data:{
                roles: arrStr
            }
        })
        res.send({success: true, message: '更新成功' , data: arrStr})
    }catch(e){
        console.log(e)
        res.fail({success: false, message: '更新失败'})
    }
}